import { defineStore } from 'pinia'
import { ref } from 'vue'
import { getHosCodeAPI, getDepartmentAPI, gainSchedulingDataAPI } from '@/api/hosp/index'
import { responseHoscodeType, hoscodeDataType, responseDepartmentType, departmentDataType, workDataType, responseWorkDataType } from '@/api/hosp/type'

export const useHospitalStore = defineStore('hospital', () => {
  // 医院预约挂号详情数据
  const hoscodeData = ref({} as hoscodeDataType)
  // 医院科室列表数据
  const departmentData = ref([] as departmentDataType[])
  // 医生信息数据
  const doctorInfo = ref({} as workDataType)

  /**
   * 获取医院预约挂号详情
   *
   * @param hoscode {string} 医院唯一 code
   */
  const getHosCode = async (hoscode: string) => {
    const { data: res }: responseHoscodeType = await getHosCodeAPI(hoscode)
    if (res.code !== 200) return ElMessage.error(res.message)
    hoscodeData.value = res.data
  }

  /**
   * 获取医院科室列表
   *
   * @param hoscode {string} 医院唯一 code
   */
  const getDepartment = async (hoscode: string) => {
    const { data: res }: responseDepartmentType = await getDepartmentAPI(hoscode)
    if (res.code !== 200) return ElMessage.error(res.message)
    departmentData.value = res.data
  }
  /** 根据排班id获取排班数据
   * @param doc { workDataType } 医生信息
   */
  const modifyDoctorInformation = async (docId: string) => {
    const { data: res }: responseWorkDataType = await gainSchedulingDataAPI(docId)
    if (res.code !== 200) return ElMessage.error(res.message)
    doctorInfo.value = res.data
  }

  return {
    hoscodeData,
    getHosCode,
    departmentData,
    getDepartment,
    doctorInfo,
    modifyDoctorInformation,
  }
})
